import geometry.*;
import java.util.*;

/**
 * @author Ryan Cassidy & Adam Blackwell
 */
// A Cartesian Point extends the functionality of Posn
class CartPt extends Posn{

  // New random Object.
  Random r = new Random();

  // Constructor.
  CartPt(int x, int y){
    super(x,y);
  }

  // Adjusts the cartpt according to a width and a height given.
  CartPt adjust(int rbound, int upbound){
    return new CartPt(fixX(this.x, rbound), fixY(this.y, upbound));
  }

  // Fixes just X.
  int fixX(int fixx, int rbound){
    if (fixx < 0)
      return 0;
    if (fixx > rbound)
      return rbound;
    return fixx;
  }

  // Fixes just Y.
  int fixY(int fixy, int upbound){
    if (fixy < 0)
      return 0;
    if (fixy > upbound)
      return upbound;
    return fixy;
  }

  // Moves a cartpt by the given dx and dy values.
  CartPt move(int dx, int dy){
    return new CartPt(this.x + dx, this.y + dy);
  }

  // Makes a random value between the bounds - 
  CartPt moveRandom(int xlow, int xhi, int ylow, int yhi){
    if ((xhi - xlow <= 0) && (yhi - ylow <= 0))
      return this.move(xlow, ylow);
    if (xhi - xlow <= 0)
      return this.move(xlow, r.nextInt(yhi - ylow) + ylow);
    if (yhi-ylow <= 0)
      return this.move(r.nextInt(xhi - xlow) + xlow , ylow);
    return this.move(r.nextInt(xhi - xlow) + xlow, r.nextInt(yhi - ylow) + ylow);
  }

  // Is the given cartpt close to this one, within the given dist? 
  boolean closeTo(CartPt pt, int dist){
    return Math.sqrt(((this.x - pt.x) * (this.x - pt.x)) +
        ((this.y - pt.y) * (this.y - pt.y))) < dist;
  }

}